package com.taskschecker;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;

public class CalendarUtils {

	private final static String[] strWeekDayNames = getWeekDayNames();

	private static String[] getWeekDayNames() {
		String[] strArr = new String[10];
		strArr[Calendar.SUNDAY] = "SUN";
		strArr[Calendar.MONDAY] = "MON";
		strArr[Calendar.TUESDAY] = "TUE";
		strArr[Calendar.WEDNESDAY] = "WED";
		strArr[Calendar.THURSDAY] = "THU";
		strArr[Calendar.FRIDAY] = "FRI";
		strArr[Calendar.SATURDAY] = "SAT";
		return strArr;
	}

	public static String getWeekDayName(int iDay) {
		return strWeekDayNames[iDay];
	}

	public static int getWeekDay(int index, int iFirstDayOfWeek) {

		int tmpWeekDay = -1;

		if (iFirstDayOfWeek == Calendar.MONDAY) {
			tmpWeekDay = index > 5 ? Calendar.SUNDAY : index + Calendar.MONDAY;
		}

		if (iFirstDayOfWeek == Calendar.SUNDAY) {
			tmpWeekDay = index + Calendar.SUNDAY;
		}

		return tmpWeekDay;

	}

	public static String getDateStr(CalendarDayCell cell) {
		Calendar cal = cell.getDate();
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
		return s.format(cal.getTime());
	}

	public static Integer getDateInt(CalendarDayCell cell) {
		Calendar cal = cell.getDate();
		SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd");
		return Integer.valueOf(s.format(cal.getTime()));
	}

	public static Integer getDateInt(int year, int month, int day) {
		Calendar c = new GregorianCalendar();
		c.set(year, month, day);
		SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd");
		return Integer.valueOf(s.format(c.getTime()));
	}

	public static String getDateYMD(Calendar date) {
		String dateStr = date.get(Calendar.YEAR) + "/";
		dateStr += date.get(Calendar.MONTH) + 1 + "/";
		dateStr += date.get(Calendar.DAY_OF_MONTH);

		return dateStr;
	}

	public static String getDateYM(Calendar date) {
		String dateStr = date.get(Calendar.YEAR) + "/";
		dateStr += date.get(Calendar.MONTH) + 1;

		return dateStr;
	}

	public static Integer getDateInt(Calendar date) {
		return getDateInt(date.get(Calendar.YEAR), date.get(Calendar.MONTH),
				date.get(Calendar.DAY_OF_MONTH));
	}

	public static Integer getCurrentDateInt() {
		Calendar date = Calendar.getInstance();
		return getDateInt(date.get(Calendar.YEAR),
				date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH));
	}

	public static String getCurrentDateStr() {
		Calendar date = Calendar.getInstance();
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
		return s.format(date.getTime());
	}

	public static HashMap<String, Integer> getCurrentDateSplit() {
		Calendar date = Calendar.getInstance();
		HashMap<String, Integer> dateMap = new HashMap<String, Integer>();
		dateMap.put("YEAR", date.get(Calendar.YEAR));
		dateMap.put("MONTH", date.get(Calendar.MONTH));
		dateMap.put("DAY", date.get(Calendar.DAY_OF_MONTH));
		return dateMap;
	}

}
